<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
//软删除
use Illuminate\Database\Eloquent\SoftDeletes;
// 按钮组 trait
use App\Models\Traits\Btn;

class Base extends Model
{
    //软删除
    use SoftDeletes,Btn;
    protected $dates = ['deleted_at'];

    //设置添加时的黑名单
    //如果你想让所有属性都可以批量赋值， 你可以将$guarded定义成一个空数组。在调用create时起作用
    protected $guarded = [];

    /**
     * 数组合并， 并加上html 标识前缀
     */
    public function treeLevel(array $data, int $pid=0, string $html='--', int $level=0) {
        static $arr = [];
        foreach($data as $val) {
            if ($pid == $val['pid']) {
                $val['html'] = str_repeat($html, $level * 2);   //str_repeat把指定字符串重复几次
                $val['level'] = $level + 1;
                $arr[] = $val;
                $this->treeLevel($data, $val['id'], $html, $val['level']);
            }
        }
        return $arr;
    }

    /**
     * 数组多层级
     */
    public function subTree(array $data, int $pid=0){
        $arr = [];
        foreach ($data as $val){
            //给定的pid是当前记录的上级ID
            if($pid == $val['pid']) {
                //递归
                $val['sub'] = $this->subTree($data,$val['id']);
                $arr[] = $val;
            }
        }
        return $arr;
    }

    /**
     * 获取所有路由
     * 网上找的这个方法，不行
     */
    public function getRouteList() {
        $routes = app('router')->getRoutes();
        // dd($routes);
        $path = [];
        foreach($routes as $k=>$value){
            $path[$k]['uir'] = $value->uir;
            $path[$k]['path'] = $value->methods[0];
        }
        return $path;
    }
}
